
window.my_editor = {
	init:function(options){
		if(options){
			tinymce.init(options);
		}else{
			tinymce.init({
				// selector: selector,
				//skin:'oxide-dark',
				language:'zh_TW',
				plugins: 'advlist autolink lists link image charmap print preview anchor searchreplace visualblocks code insertdatetime media table paste code imagetools responsivefilemanager',
				toolbar: 'undo redo |  formatselect | bold italic forecolor backcolor  | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | image responsivefilemanager filemanager link unlink code',
				// height: 650, //编辑器高度
				min_height: 400,
				//valid_elements: '*[*]',
				branding: false,
				relative_urls: false,
				forced_root_block: '',
				fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
				font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;',
				// importcss_append: true,
				//自定义文件选择器的回调内容
				toolbar_sticky: true,
				autosave_ask_before_unload: false,
				file_picker_types:['image'],
				'external_filemanager_path' : baseUrl +'/filemanager/',
				'filemanager_access_key' : apkey,
				'filemanager_title' : 'Filemanager',
				'external_plugins' : {
					'responsivefilemanager' : baseUrl +'/filemanager/responsivefilemanager/plugin.min.js',
					'filemanager' : baseUrl +'/filemanager/plugin.min.js'
				},
			});
		}		
	},
	show: function(id,selector){
		$(".tox-tinymce-aux").remove();
		tinymce.execCommand('mceRemoveEditor', true, id);
		var self = this;
		setTimeout(function(){
			// self.init(selector);
			tinymce.execCommand('mceAddEditor', false, id);
		}, 100);
	},
	hide: function(id){
		$(".tox-tinymce-aux").remove();
		tinymce.execCommand('mceRemoveEditor', true, id);
	},
	getData:function(id){
		var content = tinymce.get(id).getContent();
		return content;
	},
	modal(target_id, id){
		$.fancybox.open(this.modalTemplate(id, target_id), {
				'width': 900,
				'height': 600,
				// 'max-width': 900,
				'max-height': 600,
				'padding': 0,
				'clickOutside': 'false',
				'clickSlide':'false',
				'preventCaptionOverlap':false,
				'closeExisting' : false,
				defaultType:"zoom",
				autoFocus: false,
				arrows:false,touch: false,
				// 'autoScale': false,
				afterShow: function (instance, current) {
					console.info('done!');
				}
			});
		this.show('modal_editor_'+id, "#modal_editor_"+id);
	},
	modalTemplate(id, target_id){
		var content = $("#"+target_id).val();
		var str = `<div class="modal-content">
		<div class="modal-header">
		<h2 class="modal-title" >編輯器</h2>
		</div>
		<div class="modal-body">
		<textArea class="form-control editorWidget" id="modal_editor_${id}" name="content">${content}</textArea>
		</div>
		<div class="modal-footer">
		<button type="button" class="layui-btn layui-btn-primary" onclick="my_editor.modalClose()">关闭</button>
		<button type="button" class="layui-btn layui-btn-default" onclick="my_editor.modalCofirm('modal_editor_${id}', '${target_id}')">確認</button>
		</div>
		</div>
		`;
		return str
	},
	modalClose(){
		$(".tox-tinymce-aux").remove();
		$.fancybox.close();
	},
	modalCofirm(id,target_id){
		var data = this.getData(id);
		$("#"+target_id).val(data);
		this.modalClose();
		this.hide(id);
	}
}